<?php
require_once './libraries/utilites/functions.php';
$path = 'data/films.txt';


$full_path = realpath($path);
$films = mreadFile($path);
$rating1 = NULL;
$rating2 = NULL;
?>





<form method="POST" action="file.php">
    <table>
        <tr><td> FROM </td><td> <input type="text" name="rating1"></td><tr>
        <tr><td> TO </td><td> <input type="text" name="rating2"></td></tr>
        <tr> <td></td> <td><input type="submit" name="submit" value="Search"></td></tr>
    </table>
</form>
<?php if (isset($_POST['submit'])): ?>
    <?php $rating1 = (float) $_POST['rating1']; ?>
    <?php $rating2 = (float) $_POST['rating2']; ?>
<?php endif; ?>



<?php foreach ($films as $item): ?>
        <?php
       $a = trim($item,"\t\r\n ");
    $film = explode(";", $item);
    if($a != ""):
        ?>

        <?php if (empty($rating1) && empty($rating2)): ?>
            <?php if (isset($film[0]) || isset($film[2]) || isset($film[3])): ?>   
<?php 
    echo $film[0];
 ?>
<img src ='public/images/films/<?php if(!empty($film[1])) echo $film[1] ?>'>
                <?php if(!empty($film[2])) echo $film[2] ?>
                <?php if(!empty($film[3])) echo $film[3] ?>
                <a href="edit.php?if=<?php echo $film[0] ?>">Edit</a>

                <br>
            <?php endif; ?>
        <?php else: ?>
            <?php if (isset($film[0]) || isset($film[2]) || isset($film[3])): ?>

                <?php if (($rating1 <= (float) $film[3]) && ($rating2 >= (float) $film[2])): ?> 
                    <img src = 'public/images/films/<?php  echo $film[1] ?>'>  
                    <?php echo $film[0] ?> 
                    <?php echo $film[2] ?>
                    <?php if (!empty($film[3])) echo $film[3] ?>
                    <a href="edit.php?if=<?php echo $film[0] ?>">Edit</a>
                    <br>
            <?php endif; ?>   <?php endif; ?> <?php endif; ?><?php endif; ?>
             
<?php endforeach; ?>   
                    
